package com.example.sm.mycook.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.sm.mycook.User;

import java.util.ArrayList;
import java.util.List;

public class SQLiteUtils {
    private Context context;
    private SQLiteHelper helper;
    private SQLiteDatabase db;

    public SQLiteUtils(Context context) {
        this.context = context;
        helper = new SQLiteHelper(context);
        db = helper.getReadableDatabase();
    }

    public SQLiteDatabase getDB(){
        return db;
    }
    public SQLiteHelper getHelper(){
        return helper;
    }

    // 从表进行查找ID
//    public boolean slelectId(String ID) {
//        List<String> list = new ArrayList<>();
//        Cursor cursor = db.rawQuery("select * from move where tid = ?",
//                new String[]{ID});
//        while (cursor.moveToNext()) {
//            String id = cursor.getString(cursor.getColumnIndex("tid"));
//            list.add(id);
//        }
//        for (int i = 0; i < list.size(); i++) {
//            String id = list.get(i);
//            if (ID.equals(id)) {
//                return true;
//            }
//        }
//        return false;
//    }



    // 往表插入数据   返回-1插入失败
    public long InsertData(String tid ,String name,String phone,String types,String address,String path,String ischeck) {
        ContentValues values = new ContentValues();
        values.put("tid", tid);
        values.put("name", name);
        values.put("phone", phone);
        values.put("types", types);
        values.put("address", address);
        values.put("path", path);
        values.put("ischeck", ischeck);

        long i = db.insert("users", null, values);
        return i;
    }

    // 从move表中查找数据 并将数据获取到
//    public List<MoveBean> selectMoveGetdata(String ID) {
//        List<MoveBean> list = new ArrayList<>();
//        Cursor cursor = db.rawQuery("select * from move where tid = ?",
//                new String[]{ID});
//        while (cursor.moveToNext()) {
//            String tid = cursor.getString(cursor.getColumnIndex("tid"));
//            String title = cursor.getString(cursor.getColumnIndex("title"));
//            String image = cursor.getString(cursor.getColumnIndex("image"));
//            String pubdate = cursor.getString(cursor.getColumnIndex("pubdate"));
//            String directors = cursor.getString(cursor.getColumnIndex("directors"));
//            String casts = cursor.getString(cursor.getColumnIndex("casts"));
//            String message = cursor.getString(cursor.getColumnIndex("message"));
//            MoveBean move = new MoveBean(tid, title, image, pubdate, directors, casts, message);
//            list.add(move);
//        }
//        return list;
//    }

    // 从user表中获取全部数据
    public List<User> selectAllData() {
        List<User> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from users", null);
        while (cursor.moveToNext()) {
            String tid = cursor.getString(cursor.getColumnIndex("tid"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String phone = cursor.getString(cursor.getColumnIndex("phone"));
            String type = cursor.getString(cursor.getColumnIndex("types"));
            String address = cursor.getString(cursor.getColumnIndex("address"));
            String path = cursor.getString(cursor.getColumnIndex("path"));
            String check = cursor.getString(cursor.getColumnIndex("ischeck"));
            User user = new User(tid,name, phone, type, address,path, check);
            list.add(user);
        }
        cursor.close();
        return list;
    }

    //删除表中的数据
    public int deleteUser(String ID) {
        int rowCount = db.delete("users", "tid = ?", new String[]{ID});

        return rowCount;
    }

    public int deleteAll() {

        int rouwCount = db.delete("users", null, null);
        return rouwCount;
    }

    public void change(){
        ContentValues values=new ContentValues();
        values.put("ischeck","2");
        db.update("users",values,"_id>?",new String [] {"0"});
    }



}
